package com.bwie.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.config.RedisConfig;
import com.bwie.mapper.ParkMapper;
import com.bwie.pojo.po.Park;
import com.bwie.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@Service
public class ParkServiceImpl implements ParkService {
    @Autowired
    private ParkMapper parkMapper;

    @Override
    public List<Park> getParkList() {
        List<Park> parkList = new ArrayList<>();

        JedisPool jedisPool = RedisConfig.redisConn();
        Jedis resource = jedisPool.getResource();
        String parkKey="park_key";

        String parkListStr = resource.get(parkKey);
        if (null==parkListStr){
            List<Park> list = parkMapper.getParkList();
            for (Park park : list) {
                parkList.add(park);
            }
            resource.set(parkKey,JSONObject.toJSONString(parkList));
        }else {
            parkList = JSONObject.parseArray(parkListStr,Park.class);
        }
        RedisConfig.closeJedisOrPool(resource,jedisPool);
        return parkList;
    }
}
